<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

class ArticleCategoryLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $articleCategoryModel;

    public function _initialize()
    {
        $this->articleCategoryModel = D('ArticleCategory', 'Model');
    }

    /**
     * 获取文章分类
     */
    public function getArticleCategory()
    {
        $condition = [];
        $condition['status'] = ['eq', 1];
        return $this->articleCategoryModel->where($condition)->select();
    }

    /**
     * 获取所有父分类
     *
     * @return mixed
     */
    public function getParentArticleCategory()
    {
        $condition = [];
        $condition['parent_id'] = ['eq', 0];
        $condition['status'] = ['eq', 1];
        return $category = $this->articleCategoryModel->where($condition)->select();
    }

    /**
     * 根据parentId 获得对应子分类
     *
     * @param int $pid
     *
     * @return mixed
     */
    public function getArticleCategoryByPid($parentId = 0)
    {
        $condition = [];
        $condition['parent_id'] = ['eq', $parentId];
        $condition['status'] = ['eq', 1];
        return $this->articleCategoryModel->where($condition)->select();
    }


    /**
     * 根据categoryId 获得分类
     *
     * @param int $categoryId
     *
     * @return mixed
     */
    public function getArticleCategoryById($categoryId = 0)
    {
        $condition = [];
        $condition['category_id'] = ['eq', $categoryId];
        $condition['status'] = ['eq', 1];
        return $this->articleCategoryModel->where($condition)->find();
    }

    /**
     * 更新文章分类
     *
     * @param int   $categoryId
     * @param array $data
     */
    public function saveArticleCategory($categoryId, $data = [])
    {
        if (empty($categoryId) || empty($data)) {
            return false;
        }
        $condition = [];
        $condition['category_id'] = ['eq', $categoryId];
        return $this->articleCategoryModel->where($condition)->save($data);
    }

    /**
     * 添加文章分类
     *
     * @param array $data
     */
    public function addArticleCategory($data = [])
    {
        if (empty($data)) {
            return false;
        }
        return $this->articleCategoryModel->add($data);
    }
}
